package com.eastfair.builder.service.impl;

import com.eastfair.builder.exceptioncode.BuilderExceptionCode;
import com.eastfair.builder.service.BuilderDrawingService;
import com.eastfair.builder.vo.ReportDrawingCategoryVO;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.api.DrawingFeign;
import com.eastfair.home.dto.DrawingDTO;
import com.eastfair.home.vo.DrawingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 主场图纸分类服务.
 *
 * @author dqq
 * @date 2022 /8/8
 */
@Service
@Slf4j
public class BuilderDrawingServiceImpl implements BuilderDrawingService {

    @Resource
    private DrawingFeign drawingFeign;

    @Override
    public List<DrawingVO> getDrawingList(DrawingDTO drawingDTO) {
        log.info("getDrawingList - 查询主场图纸设置列表, drawingPageQuery={}", drawingDTO);
        R<List<DrawingVO>> result = drawingFeign.getDrawingList(drawingDTO);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(BuilderExceptionCode.HOME_DRAWING_QUERY_ERROR);
        }
        return result.getData();
    }

    @Override
    public List<DrawingVO> queryForReportSpace(DrawingDTO drawingDTO) {
        log.info("queryForReportSpace - 查询图纸列表为搭建商报馆, drawingDTO={}", drawingDTO);
        R<List<DrawingVO>> result = drawingFeign.queryForReportSpace(drawingDTO);
        log.info("queryForReportSpace - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public List<DrawingVO> listDrawingVOsByExhibitionId(Long exhibitionId, Integer isDouble, Integer isPoint) {
        DrawingDTO drawingDTO = new DrawingDTO();
        drawingDTO.setExhibitionId(exhibitionId);
        drawingDTO.setIsPoint(isPoint);
        drawingDTO.setIsDouble(isDouble);
        return queryForReportSpace(drawingDTO);
    }

    @Override
    public List<ReportDrawingCategoryVO> listReportDrawingCategoryVOsByExhibitionId(Long exhibitionId, Integer isDouble, Integer isPoint) {
        List<DrawingVO> drawingVoList = listDrawingVOsByExhibitionId(exhibitionId, isDouble, isPoint);
        if (drawingVoList == null || drawingVoList.isEmpty()) {
            return Collections.emptyList();
        }
        return drawingVoList
                .stream()
                .map(drawingVO -> {
                    ReportDrawingCategoryVO reportDrawingCategoryVO = new ReportDrawingCategoryVO();
                    reportDrawingCategoryVO.setDrawingCategoryId(drawingVO.getId());
                    reportDrawingCategoryVO.setDrawingCategoryName(drawingVO.getDrawingName());
                    reportDrawingCategoryVO.setDrawingCategorySort(drawingVO.getSort());
                    reportDrawingCategoryVO.setIsPoint(drawingVO.getIsPoint());
                    reportDrawingCategoryVO.setIsDouble(drawingVO.getIsDouble());
                    reportDrawingCategoryVO.setImageUrl(drawingVO.getImageUrl());
                    reportDrawingCategoryVO.setSpecification(drawingVO.getSpecification());
                    return reportDrawingCategoryVO;
                })
                .collect(Collectors.toList());
    }
}